﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBAccess;
using System.Data;
using System.Data.OleDb;

namespace DBAccess
{
    public class OleDbOperator : DBOperator
    {

        /// <summary>
        /// 获取实例的锁变量
        /// </summary>
        private static object lockFlag = new object();

        /// <summary>
        /// 该类的唯一实例
        /// </summary>
        private static DBOperator dbOperator;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        private OleDbOperator()
            : base()
        {
        }

        /// <summary>
        /// 获取该类的唯一实例
        /// </summary>
        /// <returns></returns>
        public static DBOperator GetDBOperatorInstance()
        {
            if (dbOperator == null)
            {
                lock (lockFlag)
                {
                    if (dbOperator == null)
                    {
                        dbOperator = new OleDbOperator();
                    }
                }
            }
            return dbOperator;
        }

        #region 覆写基类的方法

        /// <summary>
        /// 获取SQLServer数据库连接
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        protected override IDbConnection GetDBConnection(string connStr)
        {
            return new OleDbConnection(connStr);
        }

        /// <summary>
        /// 获取SQLServer对应的Command
        /// </summary>
        /// <returns></returns>
        protected override IDbCommand GetDBCommand()
        {
            return new OleDbCommand();
        }

        /// <summary>
        /// 根据Command在DataTable中填充数据
        /// </summary>
        /// <param name="command"></param>
        /// <param name="ds"></param>
        /// <param name="sqlParams"></param>
        protected override void FillQueryData(IDbCommand command, DataSet ds, params object[] sqlParams)
        {
            if (sqlParams != null && sqlParams.Length > 0)
            {
                OleDbParameter[] sqlServerParams = (OleDbParameter[])sqlParams;
                for (int i = 0; i < sqlServerParams.Length; i++)
                {
                    if (null != sqlServerParams[i])
                    {
                        command.Parameters.Add(sqlServerParams[i]);
                    }
                }
            }

            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command as OleDbCommand))
            {
                adapter.Fill(ds);
            }
        }

        /// <summary>
        /// 获取动态参数
        /// </summary>
        /// <param name="dbParams"></param>
        /// <returns></returns>
        protected override IDataParameter[] GetDBParameter(object[] dbParams)
        {
            if (dbParams != null && dbParams.Length > 0)
                return (OleDbParameter[])dbParams;
            else
                return null;
        }

        /// <summary>
        /// 创建一个带参数的Command
        /// </summary>
        /// <param name="commandType"></param>
        /// <param name="cmdText"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected override IDbCommand NewCommand(CommandType commandType, string cmdText, params IDbDataParameter[] parameters)
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = cmdText;
            cmd.CommandType = commandType;

            if (parameters != null && parameters.Length > 0)
            {
                foreach (OleDbParameter parameter in parameters)
                    cmd.Parameters.Add(parameter);
            }

            return cmd;
        }

        /// <summary>
        /// 创建动态参数
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="paramValue"></param>
        /// <param name="paramDBType"></param>
        /// <returns></returns>
        public override IDataParameter CreateParameter(string paramName, object paramValue, object paramDBType)
        {
            OleDbParameter sqlParameter = new OleDbParameter();
            sqlParameter.ParameterName = paramName;
            sqlParameter.Value = paramValue;
            sqlParameter.DbType = (DbType)paramDBType;
            return sqlParameter;
        }

        /// <summary>
        /// 创建动态参数数组
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public override IDataParameter[] CreateParameterCount(int count)
        {
            return new OleDbParameter[count];
        }

        /// <summary>
        /// 创建动态参数列表
        /// </summary>
        /// <returns></returns>
        public override List<IDataParameter> CreateParameterList()
        {
            return new List<OleDbParameter>().Cast<IDataParameter>().ToList();
        }

        #endregion
    }
}
